//
//  ResidentWorkService.m
//  homeland
//
//  Created by Alawn on 13-10-23.
//  Copyright (c) 2013年 xzysun. All rights reserved.
//

#import "ResidentWorkService.h"
#import "StreetInfoCell.h"
#import "StreetWorkEntity.h"
#import "OrgListItem.h"
#import "UserInfoService.h"
@interface ResidentWorkService()
@property (assign, nonatomic) SelectedWorkType currentSelectedType;
@end
@implementation ResidentWorkService
-(id)init
{
    self = [super init];
    if (self) {
        _dataItemName = @"StreetWorkEntity";
        self.currentSelectedType =SelectedSubject;
    }
    return self;
}

-(NSArray *)getSegmentsTitles
{
    return [NSArray arrayWithObjects:@"按街道筛选", @"按部门筛选", nil];
}

-(NSArray *)getSegmentsTitles:(int)index
{
    NSArray *array = index == 0 ? @[@"tab_selected_jd_x"] : @[@"tab_selected_bm_x"];
    return array;
}

-(void)selectSegmentBarAtIndex:(NSInteger)selected
{
    [self cancelAllRequest];
    switch (selected) {
        case 0:
            self.currentSelectedType = SelectedSubject;
            break;
        case 1:
            self.currentSelectedType = SelectedEvent;
            break;
        default:
            break;
    }
    [self loadDataWithKey:nil];
}

-(void)loadDataWithKey:(NSString *)keyOrNil
{
    _currentPageNo = 1;//刷新或查询的时候将请求页码变为1
    if (self.dataList) {
        self.dataList = nil;
    }
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    //    if (keyOrNil) {
    //        [params setValue:keyOrNil forKey:@"category"];
    //    } else {
    //        [params setValue:@"0" forKey:@"category"];
    //    }
//    [params setValue:[NSString stringWithFormat:@"%d",self.currentSelectedType] forKey:@"parentCode"];
    if (self.currentSelectedType==SelectedSubject) {
        [super doHttpPost:SERVER_URL_PEOPLE_ORG_LIST headers:nil path:nil parameters:params];
    }
    if (self.currentSelectedType==SelectedEvent) {
        [self doHttpPost:SERVER_URL_JMFW_ORGINFO headers:nil path:nil parameters:params];
    }
    //    [self doHttpPost:SERVER_URL_GUIDE_TYPE headers:nil path:nil parameters:params];
}

-(void)loadMoreDataWithKey:(NSString *)keyOrNil
{
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    //    if (keyOrNil) {
    //        [params setValue:keyOrNil forKey:@"category"];
    //    } else {
    //        [params setValue:@"0" forKey:@"category"];
    //    }
//   [params setValue:[NSString stringWithFormat:@"%d",self.currentSelectedType] forKey:@"parentCode"];
    if (self.currentSelectedType==SelectedSubject) {
        [super doHttpPost:SERVER_URL_PEOPLE_ORG_LIST headers:nil path:nil parameters:params];
    }
    if (self.currentSelectedType==SelectedEvent) {
        [self doHttpPost:SERVER_URL_JMFW_ORGINFO headers:nil path:nil parameters:params];//按部门筛选还有问题，再开发、、//todo
    }
    //    [self doHttpPost:SERVER_URL_GUIDE_TYPE headers:nil path:nil parameters:params];
}
-(void)doHttpPost:(NSString *)posturl headers:(NSDictionary*)headersOrNil path:(NSString*)path parameters:(NSDictionary *)params
{
    UserInfoService *userInfo=[UserInfoService getUserInfoService];
    if ([params objectForKey:@"userId"]==nil) {
        [params setValue:userInfo.userInfo.Id forKey:@"userId"];
    }
    MKNetworkEngine *currentEngine = [[MKNetworkEngine alloc] initWithHostName:posturl customHeaderFields:headersOrNil];
    MKNetworkOperation *op = [currentEngine operationWithPath:path params:params httpMethod:@"POST"];
    [op addCompletionHandler:^(MKNetworkOperation *operation) {
        
        BOOL isSuccess = NO;
        NSMutableArray *jsonArray = [operation responseJSON];
//        NSMutableArray *jsonArray=[NSMutableArray array];
        NSMutableArray *tempArray = [NSMutableArray array];
        if (jsonArray!=nil) {
            isSuccess = YES;//replyOpinion
            for (int i=0;i<[jsonArray count]; i++) {
//                StreetWorkEntity *tempItem=[[StreetWorkEntity alloc] init];
//                NSObject *myObj=[jsonArray objectAtIndex:i];
//                tempItem.Id=[NSString stringWithFormat:@"%@",myObj];
//                tempItem.name=[NSString stringWithFormat:@"%@",myObj];;
                
                OrgListItem *tempItem = getObjectFromDic([OrgListItem class], [jsonArray objectAtIndex:i]);
                [tempArray addObject:tempItem];
            }
            self.dataList=nil;
            self.dataList=tempArray;
        }
        //执行回调
        if (isSuccess) {
            if (self.delegate && [self.delegate respondsToSelector:@selector(loadDataFinished)]) {
                [self.delegate loadDataFinished];
            }
        } else {
            if (self.delegate && [self.delegate respondsToSelector:@selector(loadDataFaild:)]) {
                [self.delegate loadDataFaild:@"获取数据失败"];
                self.dataList=nil;
            }
        }
        
    }errorHandler:^(MKNetworkOperation *errorOp, NSError* err) {
        NSLog(@"MKNetwork request error : %@", [err localizedDescription]);
        if (self.delegate && [self.delegate respondsToSelector:@selector(loadDataFaild:)]) {
            [self.delegate loadDataFaild:@"服务器网络异常"];
        }
    }];
    [currentEngine enqueueOperation:op];
}
-(void)configCell:(UITableViewCell *)cell atIndex:(NSIndexPath *)index
{
    
    StreetInfoCell *tempCell = (StreetInfoCell *)cell;
    tempCell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    if (self.currentSelectedType==SelectedSubject)
    {
        StreetWorkEntity *item = [self.dataList objectAtIndex:index.row];
        if (item.extendProps!=(id)[NSNull null]) {
            tempCell.titleLabel.text = item.extendProps;
        } else {
            tempCell.titleLabel.text=@"";
        }
    } else if (self.currentSelectedType==SelectedEvent)
    {
        OrgListItem *item = [self.dataList objectAtIndex:index.row];
        tempCell.titleLabel.text = item.name;
    }
}

-(NSDictionary *)getTargetConfigForCellAtIndex:(NSIndexPath *)indexPath
{
//    StreetWorkEntity *item = [self.dataList objectAtIndex:indexPath.row];
//    item.categoryCode=[NSString stringWithFormat:@"%@",item.category];
//    item.ztsxfl=@"";
//    item.sjfl=@"";
//    item.tddx=@"";
//    NSString *name = item.name;
//    NSString *code = item.parentCode;
//    if ([code compare:@"1"]==NSOrderedSame) {
//        item.ztsxfl=name;
//        item.sjfl=@"";
//        item.tddx=@"";
//    }
//    if ([code compare:@"2"]==NSOrderedSame) {
//        item.ztsxfl=@"";
//        item.sjfl=name;
//        item.tddx=@"";
//    }
//    if ([code compare:@"3"]==NSOrderedSame) {
//        item.ztsxfl=@"";
//        item.sjfl=@"";
//        item.tddx=name;
//    }
    id item = [self.dataList objectAtIndex:indexPath.row];
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:@"居民办事指南列表",@"key",item,@"dataItem", nil];
    return dic;
}
@end
